<template>
  <div class="dashboard-layout">
    <el-container>
      <el-header>
        <div class="logo-box">
<!--          <el-icon class="collapse-icon" @click="handleChangeMenuCollapse">-->
<!--            <i class="iconfont icon-liebiao"></i>-->
<!--          </el-icon>-->
<!--          <el-divider direction="vertical" />-->
<!--          <span class="logo-text" @click="handleGotoIndex">{{$t('appName')}}</span>-->
          <div class="logo" @click="handleGotoIndex"></div>
        </div>
<!--        <div class="search-box">-->
<!--          <el-input-->
<!--              clearable-->
<!--              class="header-search-input"-->
<!--              v-model="searchText"-->
<!--              size="large"-->
<!--              placeholder="输入关键字搜索"-->
<!--              :suffix-icon="Search"-->
<!--          />-->
<!--        </div>-->
        <div class="menu-box">
          <div class="menu-item" :class="currentPath==='/ds/user' ? 'active' : ''" @click="gotoRoute('/ds/user')">用户管理</div>
          <div class="menu-item" :class="currentPath==='/ds/app' ? 'active' : ''" @click="gotoRoute('/ds/app')">应用管理</div>
          <div class="menu-item" :class="currentPath==='/ds/record' ? 'active' : ''" @click="gotoRoute('/ds/record')">API调用记录</div>
          <div class="menu-item" :class="currentPath==='/ds/log' ? 'active' : ''" @click="gotoRoute('/ds/log')">操作日志</div>
          <div class="menu-item" :class="currentPath==='/ds/chat' ? 'active' : ''" @click="gotoRoute('/ds/chat')">API测试</div>
        </div>
      </el-header>
      <el-container>
        <el-main class="dashboard-main"><router-view/></el-main>
      </el-container>
<!--      <el-footer>-->
<!--        <footer-comp></footer-comp>-->
<!--      </el-footer>-->
    </el-container>
  </div>
</template>

<script setup>
import {ref, provide, onMounted} from 'vue'
import {Fold, Expand, ArrowDown, Search } from '@element-plus/icons-vue'
import {ElMessage} from 'element-plus'
import FooterComp from './components/footer.vue'
import {useRouter} from "vue-router";
import api from './api/index.js'
import common from "~/common/common.js";

const user = common.getLocalUser()
const uid = user.userId
const router = useRouter()
window.__router = router
const isCollapse = ref(false)
onMounted(() => {
  document.body.classList.remove('index-body')
})
const handleChangeMenuCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const searchText = ref('')

const currentPath = ref(router.currentRoute.value.fullPath)

const userInfoForm = ref({
  nickname: '',
  icon: '',
})
const userInfo = ref({
  nickname: '',
  icon: '',
})
provide('userInfo', userInfo)


const handleGotoIndex = () => {
  // router.replace('/ds/user')
}
const gotoRoute = (path) => {
  currentPath.value = path
  router.push(path)
}

const userInfoDialogVisible = ref(false)
const handleDropMenuClick = (command) => {
  switch(command) {
    case 'info-change':
      getUserInfo()
      userInfoDialogVisible.value = true
      break;
    case 'logout':
      common.removeLocalUser()
      common.removeLocalToken()
      router.replace('/login')
      break;
  }
}
let avatarFile = null
const handleFileChange = (evt) => {
  const file = evt.target.files[0]
  avatarFile = file
  let fileResult = ''
  const reader = new FileReader()
  reader.readAsDataURL(file)
  reader.onload = () => {
    fileResult = reader.result
  }
  reader.onloadend = () => {
    userInfoForm.value.icon = fileResult
    // console.log('handleFileChange', fileResult)
  }
}
const saveUserInfoLoading = ref(false)
const handleSaveUserInfo = () => {
  // if (!userInfoForm.value.icon || !userInfoForm.value.nickname) {
  //   ElMessage.error('请补充头像和昵称信息')
  //   return
  // }
  saveUserInfoLoading.value = true
  const formData = new FormData();
  if (avatarFile) {
    formData.append('icon', avatarFile)
  }
  api.createUserInfo({
    uid,
    nickname: userInfoForm.value.nickname
  }, formData).then(res => {
    saveUserInfoLoading.value = false
    userInfoDialogVisible.value = false
    if (res.status === 'success') {
      userInfo.value.nickname = userInfoForm.value.nickname
      userInfo.value.icon = userInfoForm.value.icon
      ElMessage.success('保存成功')
    } else {
      ElMessage.error('保存失败')
    }
  }).catch(e => {
    saveUserInfoLoading.value = false
    ElMessage.error('保存异常')
    console.log(e)
  })
}

</script>

<style scoped lang="scss">
  .dashboard-layout{
    position: relative;
    width: 100%;
    height: 100%;
  }
  .el-container{
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: transparent;
  }
  .el-header{
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80px;
    .logo-box{
      display: flex;
      align-items: center;
      font-size: 2.4rem;
    }
    .logo{
      background: url("./assets/images/logo.png") no-repeat;
      background-size: 100% 100%;
      width: 144px;
      height: 52px;
      cursor: pointer;
    }
    .menu-box{
      position: absolute;
      display: flex;
      align-items: center;
      left: 15%;
      height: 100%;
      top:0;
      .menu-item{
        position: relative;
        font-size: 1.2rem;
        cursor: pointer;
        margin-right: 4rem;
        display: inline-block;
        color: var(--el-text-color-primary);
        &:after{
          content: '';
          position: absolute;
          top: calc(100% + 5px);
          width: 0;
          right: 0;
          height: 3px;
          -webkit-transition: width .2s cubic-bezier(0.29, 0.18, 0.26, 0.83);
          -o-transition: width .2s cubic-bezier(0.29, 0.18, 0.26, 0.83);
          transition: width .2s cubic-bezier(0.29, 0.18, 0.26, 0.83);
          background: var(--primary-color);
        }

      }
      .menu-item:hover:after,.menu-item.active:after{
        background: var(--primary-color);
        width: 100%;
        left: 0;
      }
    }
    .logo-text{
      cursor: pointer;
    }
    .collapse-icon{
      cursor: pointer;
    }
    .icon-liebiao{
      font-size: 2.2rem;
    }
    .header-search-input{
      width: 32rem;
    }
  }
  .el-aside{
    //background: rgb(60 64 73);
  }
  .el-menu{
    height: 100%;
    border-right: none;
    //background: none;
    background: rgb(60 64 73);
  }
  .el-menu-item{
    color: #fff;
    &:hover{
      background-color: rgb(56 159 238);
    }
    &.is-active{
      color: #fff;
      background-color: rgb(56 159 238);
    }
  }
  .user-box{
    display: flex;
    align-items: center;
    justify-content: space-between;

    .icon-xiaoxitixing{
      font-size: 2rem;
      color: var(--el-text-color-secondary);
    }
    .el-divider{
      margin: 0 20px;
    }
  }
  .dashboard-main{
    background: #f5f5f9;
    padding: 0;
  }
  .user-info{
    position: relative;
    display: flex;
    align-items: center;
    cursor: pointer;
    .avatar-bg{
      position: relative;
      width: 3rem;
      height: 3rem;
      border-radius: 3rem;
      background: #f7f7f7;
    }
    .avatar{
      width: 100%;
      height: 100%;
      border-radius: 3rem;
      background-size: 100% 100%;
    }
    .name{
      margin: 0 10px;
    }
  }
  .user-info-content{
    position: relative;
    //padding: 2rem 3.5rem;
  }
  .user-avatar-content{
    position: relative;
    display: flex;
    justify-content: center;
  }
  .avatar-box {
    position: relative;
    width: 16rem;
    height: 16rem;
    min-height: 16rem;
    background: #f7f7f7;
    border-radius: 16rem;
    cursor: pointer;

    .avatar {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      border-radius: 16rem;
      background-repeat: no-repeat;
      background-size: 100% 100%;
    }

    .avatar-mask {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      border-radius: 16rem;
      background: rgba(0, 0, 0, 0.1);
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      opacity: 0;

      .iconfont {
        font-size: 3.0rem;
        color: #fff;
      }
    }

    .file-input {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      border-radius: 16rem;
      opacity: 0;
      cursor: pointer;
    }

    &:hover {
      .avatar-mask {
        opacity: 1;
      }
    }
  }
  .form-row{
    position: relative;
    margin-top: 20px;
    display: flex;
    align-items: center;
    .label{
      width: 80px;
      min-width: 80px;
    }
  }
  .el-footer{
    height: 50px;
    padding: 0;
  }
</style>
